package com.zfl;

import java.util.HashMap;
import java.util.Map;

public class SubarraySum {

    public int subarraySum(int[] nums, int k) {
        int sum = 0;
        int count=0;
        Map<Integer,Integer> sum2Count = new HashMap<>();
        sum2Count.put(0,1);

        for (int num : nums) {
            sum+=num;

            count+=sum2Count.getOrDefault(sum-k,0);
            sum2Count.put(sum,sum2Count.getOrDefault(sum,0)+1);
        }

        return count;
    }
}
